//
//  InputRouterViewController.m
//  SenMo
//
//  Created by ZengYuan on 15/10/12.
//  Copyright © 2015年 ZengYuan. All rights reserved.
//

#import "InputDeviceViewController.h"
#import "DeviceHandle.h"
#import "InputDeviceAdmViewController.h"


@interface InputDeviceViewController ()
{
    MBProgressHUD *hud;
    __weak IBOutlet UITextField *LCodeField;
    __weak IBOutlet UIButton    *sureBtn;

}
@end

@implementation InputDeviceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.titleLabel.text=@"添加设备";
    
    
    if(self.qrcode_device_id.length>0){
        LCodeField.text = self.qrcode_device_id;

        [self performSelector:@selector(sureAction:) withObject:nil afterDelay:0.5];
    }
    
    
    UIImage *streImage = [[UIImage imageNamed:@"y_btn"] resizableImageWithCapInsets:UIEdgeInsetsMake(6, 10, 6, 10)];
    [sureBtn setBackgroundImage:streImage forState:UIControlStateNormal];
    
}

- (IBAction)sureAction:(id)sender {

    if(LCodeField.text.length>0){
        
        UIWindow *window=[[UIApplication sharedApplication].windows lastObject];
        
        hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
        hud.labelText=@"正在绑定设备";

//        dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
//            // Do something...
////            sleep(2);
            [self bindDevice];
//
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [MBProgressHUD hideHUDForView:window animated:YES];
//            });
//        });
        
//        imgView.hidden = NO;
//        nameLabel.hidden = NO;
//        addBtn.hidden=NO;

    }else{
        MBALERT(@"L码不能为空");
    }

    
    
}

-(void)bindDevice{

    __weak typeof(self) weakself = self;
    
    [DeviceHandle deviceBind:LCodeField.text
                      result:^(NSInteger sta) {
        
                          if (sta == 1) {
                              
                              hud.mode = MBProgressHUDModeText;
                              hud.labelText=@"此设备已被绑定，请等待设备管理员确认";
                              [hud hide:YES afterDelay:1] ;
//                              MBALERT(@"此设备已被绑定，请等待设备管理员确认");
                              [weakself.navigationController popToRootViewControllerAnimated:YES];

                          }else if (sta == 2){
                              
                              [hud hide:YES afterDelay:1] ;
                              
                              InputDeviceAdmViewController *evc = [InputDeviceAdmViewController fromSB:@"InputDeviceAdmViewController"];
                              evc.device_id = LCodeField.text;
                               [weakself.navigationController pushViewController:evc animated:YES];
                              
                          }else  if (sta == 0){
                              
                              hud.mode = MBProgressHUDModeText;
                              hud.labelText=@"此设备已被绑定";
                              [hud hide:YES afterDelay:1] ;
                              
                          }
     
//        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]] ;
//        hud.mode = MBProgressHUDModeCustomView;
//        hud.labelText=@"绑定成功";
//        [hud hide:YES afterDelay:1] ;
//        
//
//        [weakself.navigationController popToRootViewControllerAnimated:YES];
        
        
    } failure:^(NSString *error) {
        hud.mode = MBProgressHUDModeText;
        hud.labelText=error;
        [hud hide:YES afterDelay:1] ;
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)addDevice:(id)sender {
}
@end
